//
//  NewsVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/8/6.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "NewsVC.h"
#import "NewCell.h"
#import "WYPublicWeb.h"
//消息详细
#define NEW_DETAIL_URL @"api/ApiMisc/getSysMsgDetail?id="
//获取系统消息
#define NEWS_URL @"/api/ApiMisc/getSysMsgs"
@interface NewsVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    //数据源
    NSMutableArray *_data;

}
@end

@implementation NewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _data = [NSMutableArray array];
    [self loadCustomNaviBar];
    [self loadTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [_data removeAllObjects];
    [self requestForNews];
}
//请求系统消息
- (void)requestForNews
{
    [WYPublic addHudToWindow];
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,NEWS_URL] Parameters:nil successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if ([ret integerValue] == 0) {
            NSArray *list = dic[@"data"];
            for (NSDictionary *new in list) {
                NewsModel *model = [[NewsModel alloc]init];
                model.news_id = new[@"id"];
                model.news_title = new[@"title"];
                model.news_time = new[@"create_time"];
                if ([[WYDataBase sharedDataBase] isExitInTabNew:model.news_id]) {
                    model.isScanfedFlag = [[WYDataBase sharedDataBase]getNewsModelById:model.news_id].isScanfedFlag;
                }
                [_data addObject:model];
            }
            [_tableView reloadData];
        }
        [WYPublic removeHudToWindow];
    } failedBlock:^{
        [WYPublic removeHudToWindow];
    } needLog:NO];
}

//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"系统消息";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
//加载表格
- (void)loadTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, WYMHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"NewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 45.0;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];

}
#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count > 0) {
        NewsModel *model = _data[indexPath.row];
        [cell fillCellWithModel:model];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_data.count > 0) {
        NewsModel *model = _data[indexPath.row];
        //更改浏览记录
        [[WYDataBase sharedDataBase]changeScanfFlagWithNewId:model.news_id];
        //跳往消息详情
        WYPublicWeb *web = [[WYPublicWeb alloc]init];
        web.mtitle = @"消息详情";
        NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
        web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,NEW_DETAIL_URL,model.news_id];
        [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
